import datetime
import json
import random
import time
import redis
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger, InvalidPage
from django.http import HttpResponse
from users.ser import *
from users.models import *
from rest_framework.views import APIView, Response


class UserPayStub(APIView):
    # TODO 等用户模块优化 搜索
    def get(self, request):
        search = request.data.get("search")
        search = search.split(" ")
        staff_name = search[0]
        staff_name = search[1]
        pass


class PayStubs(APIView):
    def get(self, request):
        page = request.GET.get('page')  # 当前页
        pagesize = request.GET.get('pagesize')  # 每页数量

        gongzi = GongZi.objects.filter().all()  # 数据
        # TODO 工资条的序列化器
        paginator = Paginator(gongzi, pagesize)  # 第一个参数是数据 第二个参数每一页的页数
        try:
            gongzis = paginator.page(page)
        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            gongzis = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
             return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            gongzis = paginator.page(paginator.num_pages)

        return Response(data=gongzis)

    def post(self, request):
        staff = request.data.get("staff")
        participation = request.data.get("participation")
        absenteeism = request.data.get("absenteeism")
        ask_off = request.data.get("ask_off")
        base_pay = request.data.get("base_pay")
        awge_jobs = request.data.get("base_pay")

        try:
            GongZi.objects.create(user=staff, chuqin=participation, kuanggong=absenteeism, qingjia=ask_off
                                  , jiben=base_pay, gangwei=awge_jobs)
        except Exception as e:
            print(e)
            return Response(data=dict(code=400, msg="新增工资条的错误信息"))
        return Response(data=dict(code=200, msg="新增工资条成功"))

    def put(self, request):
        staff = request.data.get("staff")
        participation = request.data.get("participation")
        absenteeism = request.data.get("absenteeism")
        ask_off = request.data.get("ask_off")
        base_pay = request.data.get("base_pay")
        awge_jobs = request.data.get("base_pay")

        try:
            GongZi.objects.filter(user=staff).update(chuqin=participation, kuanggong=absenteeism, qingjia=ask_off,
                                                     jiben=base_pay, gangwei=awge_jobs)
        except Exception as e:
            print(e)
            return Response(data=dict(code=400, msg="修改工资条的错误信息"))
        return Response(data=dict(code=200, msg="修改工资条成功"))

    def delete(self, request):
        id = request.date.get('id')

        try:
            GongZi.objects.filter(user=id).delete()
        except Exception as e:
            print(e)
            return Response(data=dict(code=400, msg="删除工资条的错误信息"))
        return Response(data=dict(code=200, msg="删除工资条成功"))


class UserPayStubs(APIView):
    def get(self, request):
        try:
            user = Users.objects.filter().all()
            ser = UsersSerializer(user, many=True)
            return Response(data=dict(code=200, msg="获取成功", data=ser.data))

        except Exception as e:
            print(e)
            return Response(data=dict(code=400, msg="获取失败"))



